package ThirdPagh;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import SHU.water2.R;
import SHU.water2.Spinnerstyle;

import static SHU.water2.loginin.connection;
import static Secondpagh.pumpMap.siteState;

public class pumpList extends AppCompatActivity implements View.OnClickListener {
    private String[] town;
    private String[] point;
    private String sqlStatement;

    private Handler handler;
    private HashMap<String, Object> item;
    private List<HashMap<String, Object>> data;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pump);
        ImmersionBar.with(this).statusBarColor(R.color.color).fitsSystemWindows(true).init();
        handler = new Handler() {
            //接收消息
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);

                setView();
                spinerset();
            }
        };
        sqlStatement = "select * from pump_site";
        connect(sqlStatement);

        click();

    }

    private void click() {
//        findViewById(R.id.re1).setOnClickListener(this);
        findViewById(R.id.add1).setOnClickListener(this);
        findViewById(R.id.ret1).setOnClickListener(this);
        findViewById(R.id.button1).setOnClickListener(this);
    }

    private void connect(String sql) {
        ArrayList<String> L1 = new ArrayList<>();
        ArrayList<String> L2 = new ArrayList<>();
        ArrayList<String> L3 = new ArrayList<>();

        ArrayList<String> L5 = new ArrayList<>();
        //新建线程连接数据库，防止主界面卡死
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Statement statement = connection.createStatement();
                    String sql;
                    Message msg = new Message();
                    // 创建用来执行sql语句的对象
                    if (sqlStatement.equals("select * from pump_site")) {
                        if (siteState.equals("open"))
                            sql = "select * from pump_site where status = 1";
                        else if (siteState.equals("close"))
                            sql = "select * from pump_site where status = 2";
                        else sql = "select * from pump_site where status = 0";
                        // 执行sql查询语句并获取查询信息
                        ResultSet rSet = statement.executeQuery(sql);

                        while (rSet.next()) {
                            L1.add(rSet.getString("site_name"));
                            L2.add(rSet.getString("location_zhen"));
                            L3.add(rSet.getString("location_point"));
                            L5.add(rSet.getString("status"));
                        }

                        ArrayList<String> townA = new ArrayList<>();
                        ArrayList<String> pointA = new ArrayList<>();
                        int townCheck = 1;
                        int pointCheck = 1;
                        townA.add((String) L2.get(0));
                        pointA.add((String) L3.get(0));
                        for (int i = 1; i < L3.size() - 1; i++) {
                            if (!townA.get(townCheck - 1).equals((String) L2.get(i))) {
                                townA.add((String) L2.get(i));
                                townCheck++;
                            }
                            if (!pointA.get(pointCheck - 1).equals((String) L3.get(i))) {
                                pointA.add((String) L3.get(i));
                                pointCheck++;
                            }
                        }

                        town = new String[townA.size()];
                        point = new String[pointA.size()];
                        for (int i = 0; i < townA.size(); i++) {
                            town[i] = (String) townA.get(i);
                        }
                        for (int i = 0; i < pointA.size(); i++) {
                            point[i] = (String) pointA.get(i);
                        }


                        data = new ArrayList<HashMap<String, Object>>();

                        for (int i = 0; i < L1.size(); i++) {
                            item = new HashMap<String, Object>();
                            item.put("name", L1.get(i));
                            //item.put("location_zhen", L2.get(i));
                            item.put("location_xu", L3.get(i));
                            //item.put("location", L4.get(i));

                            if (L5.get(i).equals("1")) {
                                item.put("status", "开");
                            } else if (L5.get(i).equals("2"))
                                item.put("status", "关");
                            else item.put("status", "掉线");

                            data.add(item);
                        }
                        handler.sendMessage(msg);
                    }

                    if (sqlStatement.startsWith("site_name=", 0)) {
                        ArrayList<String> L1 = new ArrayList<>();
                        ArrayList<String> L2 = new ArrayList<>();
                        ArrayList<String> L3 = new ArrayList<>();

                        String sql1 = "select * from pump_site where location_point = '" + sqlStatement.substring(10) + "'";
                        ResultSet rSet1 = statement.executeQuery(sql1);

                        while (rSet1.next()) {
                            L1.add(rSet1.getString("site_name"));
                            L2.add(rSet1.getString("location_point"));
                            L3.add(rSet1.getString("status"));
                        }

                        data = new ArrayList<HashMap<String, Object>>();

                        for (int i = 0; i < L1.size(); i++) {
                            HashMap<String, Object> item = new HashMap<String, Object>();
                            item.put("name", L1.get(i));
                            item.put("location_xu", L2.get(i));

                            if (L3.get(i).equals("1")) {
                                item.put("status", "开");
                            } else if (L3.get(i).equals("2"))
                                item.put("status", "关");
                            else item.put("status", "掉线");
                            data.add(item);
                        }
                        handler.sendMessage(msg);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    public void setView() {
        //创建SimpleAdapter适配器将数据绑定到item显示控件上
        SimpleAdapter adapter = new SimpleAdapter(pumpList.this, data, R.layout.pumplist,
                new String[]{"name", "location_xu", "status"}, new int[]{R.id.name, R.id.location, R.id.status});

        ListView listView = (ListView) findViewById(R.id.list1);
        //实现列表的显示
        listView.setAdapter(adapter);
    }


    public void spinerset() {

        Spinner spinner1 = findViewById(R.id.spinner1);
        Spinner spinner2 = findViewById(R.id.spinner2);

        ArrayAdapter<String> arr_adapter1 = new Spinnerstyle(pumpList.this, town);
        //设置样式
        arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner1.setAdapter(arr_adapter1);
        ArrayAdapter<String> arr_adapter2 = new Spinnerstyle(pumpList.this, point);
        //设置样式
        arr_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner2.setAdapter(arr_adapter2);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sqlStatement = "site_name=" + arr_adapter2.getItem(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    //按钮设置
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.re1:
//                read();
//                break;

            case R.id.button1:
                read();
                break;

            case R.id.ret1:
                finish();
                break;
        }
    }

    public void read() {
        /*if (getdata == false) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("尚未读回数据，请刷新重试").setPositiveButton("是", null).show();
        } //else*/
        connect(sqlStatement);
    }


}
